#!usr/bin/env python
# -*- coding:utf-8 -*-
# user nanme: Mr.zhang
# data time : 2022/11/22   2:56 下午
# project   : install_apk.py
# IDE       : PyCharm
# email     :17600960626@163.com
import logging
import subprocess
import time
from multiprocessing import Pool

from driver.conf_devices import devices
from public.read_apk import rad_apk




class ins_apk(devices):

    def quickinstall(self,device):  # 安装apk
        try:
            for i in rad_apk.read_apk():
                script = 'adb -s ' + device + ' install -g ' + i
                res = subprocess.run(script, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True,
                                         universal_newlines=True)
                code = res.returncode
        except Exception as e:
            logging.warning("安装失败️:%s\n"%e,device)
            raise
        else:
            if code == 0:
                logging.warning("安装成功,code值=%s" % code)
            else:
                logging.error('code值异常，异常devices：%s'%device)



    def qainstall(self,devices):
        starttime = time.time()
        pool = Pool(len(devices))  # 安装apk创建任务池
        pool.map(self.quickinstall, devices)
        endtime = time.time()
        pool.close()
        pool.join()
        times = (endtime - starttime)
        logging.warning("执行时间:%ss"%round(times,2))  # 打印时间


    def install_apk(self):
        """多线程入口"""
        try:
            # print(self.devlists())
            self.qainstall(self.devlists())
        except Exception as e:
            logging.error("更新失败",e)

